<?php
/**
 * Created by PhpStorm.
 * User: aDaddy
 * Date: 2016/7/25
 * Time: 9:55
 */

namespace App\Model\LazyCommon;

use App\Base\BaseConfig;
use App\Base\BaseModel;
use Doctrine\Bundle\DoctrineBundle\Registry;
use App\Entity\AdminMenuUser;
use App\Setting\LazyCommon\AdminMenuUserSetting;
use App\Entity\UserGroupsRole;
use Symfony\Component\HttpFoundation\Request;

class AdminMenuUserModel extends BaseModel{


    /*
     * 列表基本设置【通用模块必须】
     */
    public function listSettingDefault()
    {
        $settings = new AdminMenuUserSetting($this->getDoctrine());
        return $settings->listConfig();
    }

    //获取所有分类
    public function getAllAdminMenuUser(){
        $qb = $this->getQueryBuilder();
        $qb->select("*")
        ->from("Admin_Menu_User");
        return $qb->execute()->fetchAll();
    }

    //获取所有分类并构建分类树
    public function getAllAdminMenuUserTree()
    {
        $categories = $this->getAllAdminMenuUser();
        $AdminMenuUserTree = $this->buildeCatTreeArray($categories);
        $data = array();
        foreach ($AdminMenuUserTree as $class) {
            $prefix = "";
            if ($class["level"] > 0) {
                $prefix .="├";
                for ($i=0;$i<$class["level"];$i++ ) {
                    $prefix .="─";
                }
            }
            $data[] = array(
                "id"=>$class["id"],
                "name"=>$prefix.$class["name"],
            );
        }
        return $data;
    }

    //获取第一层分类
    public function getRootAdminMenuUser()
    {
        $qb = $this->getQueryBuilder();
        $qb->select("*")
            ->where("level = ?")
            ->setParameter(0,"1")
            ->from("Admin_Menu_User");
        return $qb->execute()->fetchAll();
    }
    /*
     * 通用Rep
     */
    public function getRep()
    {
        $settings = new AdminMenuUserSetting($this->getDoctrine());
        return $this->em->getRepository($settings->bundleName.":".$settings->entityName);
    }

    public function getAdminListsJson()
    {
        $allData = $this->getAllAdminMenuUser();
        $allData = $this->buildeCatTreeArray($allData);
        $env = BaseConfig::__ENV__;
        if ($env == "dev") {
            foreach ($allData as &$row) {
                if (strlen($row["link"]) > 4){
                    $row["link"] = "/app_dev.php".$row["link"];
                }
            }
        }
//        var_dump($allData);
        return $this->getSonFromArray($allData,0);
    }

    private function getSonFromArray($allData , $pid)
    {
        $data = array();
        foreach ($allData as $row) {
            if ($row["parent"] == $pid){
                $row["sons"] = $this->getSonFromArray($allData,$row["id"]);
                $data[] = $row;
            }
        }
        return $data;
    }



    /**
     * @param $user
     * @return AdminMenuUser[]
     */
    public function getByAdminMenuByUser($user)
    {
        /**@var $list AdminMenuUser[] */
        $list = $this->getRep()->findBy(array("user"=>$user));
        return $list;
    }

    public function getMenu4Groups($id)
    {
        /**@var $list AdminMenuUser[] */
        $list = $this->getByAdminMenuByUser($id);
        $menus = [];
        foreach ($list as $item) {
            $menus[] = $item->getMenu();
        }
        return $menus;
    }

    public function update($user,$menus)
    {
        $orgList = $this->getByAdminMenuByUser($user);
        foreach ($orgList as $one) {
            $this->em->remove($one);
        }
        foreach ($menus as $menu) {
            $AdminMenuUser = new AdminMenuUser();
            $AdminMenuUser->setMenu($menu)->setUser($user);
            $this->em->persist($AdminMenuUser);
        }
        $this->em->flush();
    }

}